<?php

/**
 * This is the model class for table "tbl_CustomerPayment".
 *
 * The followings are the available columns in table 'tbl_CustomerPayment':
 * @property integer $customerID
 * @property integer $seqNo
 * @property integer $paymentMethodID
 * @property integer $creditCardTypeID
 * @property string $CustomerIPAddress
 * @property string $creditCardNumber
 * @property string $creditSecureCode
 * @property integer $creditExpireMonth
 * @property integer $creditExpireYear
 * @property integer $sameNameInd
 * @property string $cardHolderFirstName
 * @property string $cardHolderLastName
 * @property string $cardHolderAddress
 * @property string $cardHolderCity
 * @property string $cardHolderStateID
 * @property string $cardHolderZipCode
 * @property string $chargedAmount
 * @property string $createdDate
 * @property string $chargedBy
 * @property string $enteredBy
 * @property string $location
 * @property string $comment
 * @property double $commissionRate
 */
class TblCustomerPayment extends CActiveRecord
{
	public $checkbox;	
	public $ddMonths = array(
		'01'=>'Jan',
		'02'=>'Feb',
		'03'=>'Mar',
		'04'=>'Apr',
		'05'=>'May',
		'06'=>'Jun',
		'07'=>'Jul',
		'08'=>'Aug',
		'09'=>'Sep',
		'10'=>'Oct',
		'11'=>'Nov',
		'12'=>'Dec');
	
	
	public $ddYears;
	
	public function init() {
		// Make dropdown list for years
		for ($i = date("Y"); $i<=date("Y")+10; $i++)
			$this->ddYears[$i] = $i;

	}
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return TblCustomerPayment the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_CustomerPayment';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('creditCardTypeID, creditCardNumber, creditSecureCode, cardHolderFirstName, cardHolderLastName, cardHolderAddress, cardHolderCity, cardHolderStateID, cardHolderZipCode, chargedAmount', 'required'),
			array('customerID, seqNo, paymentMethodID, creditCardTypeID, creditExpireMonth, creditExpireYear, sameNameInd, cardHolderZipCode, creditCardNumber, creditSecureCode', 'numerical', 'integerOnly'=>true),
			array('chargedAmount', 'numerical', 'integerOnly'=>false ),
			array('commissionRate', 'numerical'),
			array('CustomerIPAddress', 'length', 'max'=>20),
			array('creditCardNumber, cardHolderAddress, chargedBy', 'length', 'max'=>50),
			array('creditSecureCode', 'length', 'max'=>3),
			array('cardHolderFirstName, cardHolderLastName, cardHolderCity', 'length', 'max'=>30),
			array('cardHolderStateID', 'length', 'max'=>2),
			array('cardHolderZipCode', 'length', 'max'=>5),
			array('chargedAmount', 'length', 'max'=>9),
			array('enteredBy, location', 'length', 'max'=>80),
			array('comment', 'length', 'max'=>1024),
			array('createdDate, checkbox, ddMonths, ddYears', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('customerID, seqNo, paymentMethodID, creditCardTypeID, CustomerIPAddress, creditCardNumber, creditSecureCode, creditExpireMonth, creditExpireYear, sameNameInd, cardHolderFirstName, cardHolderLastName, cardHolderAddress, cardHolderCity, cardHolderStateID, cardHolderZipCode, chargedAmount, createdDate, chargedBy, enteredBy, location, comment, commissionRate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'customerID' => 'Customer',
			'seqNo' => 'Seq No',
			'paymentMethodID' => 'Payment Method',
			'creditCardTypeID' => 'Credit Card Type',
			'CustomerIPAddress' => 'Customer Ipaddress',
			'creditCardNumber' => 'Credit Card Number',
			'creditSecureCode' => 'Credit Secure Code',
			'creditExpireMonth' => 'Credit Expire Month',
			'creditExpireYear' => 'Credit Expire Year',
			'sameNameInd' => 'Same Name Ind',
			'cardHolderFirstName' => 'Card Holder First Name',
			'cardHolderLastName' => 'Card Holder Last Name',
			'cardHolderAddress' => 'Card Holder Address',
			'cardHolderCity' => 'Card Holder City',
			'cardHolderStateID' => 'Card Holder State',
			'cardHolderZipCode' => 'Card Holder Zip Code',
			'chargedAmount' => 'Charged Amount',
			'createdDate' => 'Created Date',
			'chargedBy' => 'Charged By',
			'enteredBy' => 'Entered By',
			'location' => 'Location',
			'comment' => 'Comment',
			'commissionRate' => 'Commission Rate',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('customerID',$this->customerID);
		$criteria->compare('seqNo',$this->seqNo);
		$criteria->compare('paymentMethodID',$this->paymentMethodID);
		$criteria->compare('creditCardTypeID',$this->creditCardTypeID);
		$criteria->compare('CustomerIPAddress',$this->CustomerIPAddress,true);
		$criteria->compare('creditCardNumber',$this->creditCardNumber,true);
		$criteria->compare('creditSecureCode',$this->creditSecureCode,true);
		$criteria->compare('creditExpireMonth',$this->creditExpireMonth);
		$criteria->compare('creditExpireYear',$this->creditExpireYear);
		$criteria->compare('sameNameInd',$this->sameNameInd);
		$criteria->compare('cardHolderFirstName',$this->cardHolderFirstName,true);
		$criteria->compare('cardHolderLastName',$this->cardHolderLastName,true);
		$criteria->compare('cardHolderAddress',$this->cardHolderAddress,true);
		$criteria->compare('cardHolderCity',$this->cardHolderCity,true);
		$criteria->compare('cardHolderStateID',$this->cardHolderStateID,true);
		$criteria->compare('cardHolderZipCode',$this->cardHolderZipCode,true);
		$criteria->compare('chargedAmount',$this->chargedAmount,true);
		$criteria->compare('createdDate',$this->createdDate,true);
		$criteria->compare('chargedBy',$this->chargedBy,true);
		$criteria->compare('enteredBy',$this->enteredBy,true);
		$criteria->compare('location',$this->location,true);
		$criteria->compare('comment',$this->comment,true);
		$criteria->compare('commissionRate',$this->commissionRate);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	public function getPaymentType()
	{
		$sql = "SELECT paymentMethod FROM tbl_PaymentMethod WHERE paymentMethodID = :paymentMethodID";
		$command = Yii::app()->db->createCommand($sql);
		$command->bindValue(':paymentMethodID', $this->paymentMethodID);
		$type = $command->queryScalar();
		
		if($type === false)
		{
			$type = 'Unknow';
		}
		
		return $type;
	}
	
	public static function getSeqNo($customerID)
	{
		$sql = "select seqNo from tbl_CustomerPayment where customerID = :customerID order by createdDate DESC";
		$command = Yii::app()->db->createCommand($sql);
		
		$command->bindValue(":customerID", $customerID);
		
		$value = $command->queryScalar();

		$seqNo = 1;
		if($value)
		{
			$seqNo = $value + 1; 
		}
		
		return $seqNo;
	}
}